package com.tiger.bmpeb.sample65;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.ObjectNotFoundException;
import javax.ejb.RemoveException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class CustomerBean04 implements EntityBean {

	private static final long serialVersionUID = 1L;

	private EntityContext entityContext;

	private CustomerID customerID;

	private String name;

	private int age;

	public CustomerID getCustomerID() {
		return customerID;
	}

	public void setCustomerID(CustomerID customerID) {
		this.customerID = customerID;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public CustomerID ejbCreate(CustomerID customerID, String name, int age) throws CreateException {
		setCustomerID(customerID);
		setName(name);
		setAge(age);

		Connection con = null;
		PreparedStatement ps = null;

		try {
			InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/Database");
			con = ds.getConnection();
			ps = con.prepareStatement("INSERT INTO CUSTOMERBEAN04 (CUSTOMERID,NAME,AGE) VALUES(?,?,?)");
			ps.setInt(1, customerID.getCustomerID());
			ps.setString(2, name);
			ps.setInt(3, age);
			if (ps.executeUpdate() != 1) {
				throw new CreateException("False create Customer04 Bean...");
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
				if (con != null) {
					con.close();
				}
			} catch (SQLException ex) {
				throw new EJBException(ex);
			}
		}
		return customerID;
	}

	public void ejbPostCreate(CustomerID customerID, String name, int age) {

	}

	public void setEntityContext(EntityContext entityContext) throws EJBException, RemoteException {
		this.entityContext = entityContext;

	}

	public void unsetEntityContext() throws EJBException, RemoteException {
		this.entityContext = null;
	}

	public CustomerID ejbFindByPrimaryKey(CustomerID customerID) throws FinderException {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/Database");
			con = ds.getConnection();
			ps = con.prepareStatement("SELECT CUSTOMERID,NAME,AGE FROM CUSTOMERBEAN04 WHERE CUSTOMERID = ?");
			ps.setInt(1, customerID.getCustomerID());
			rs = ps.getResultSet();
			if (!rs.next()) {
				throw new ObjectNotFoundException("cannot find CuntomerBean04..");
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
				if (con != null) {
					con.close();
				}
			} catch (SQLException ex) {
				throw new EJBException(ex);
			}
		}

		return customerID;
	}

	public void ejbLoad() throws EJBException, RemoteException {
		CustomerID customerID = (CustomerID) entityContext.getPrimaryKey();
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/Database");
			con = ds.getConnection();
			ps = con.prepareStatement("SELECT NAME,AGE FROM CUSTOMERBEAN04 WHERE CUSTOMERID = ?");
			ps.setInt(1, customerID.getCustomerID());
			rs = ps.getResultSet();
			if (!rs.next()) {
				throw new EJBException("cannot find CuntomerBean04..");
			} else {
				setCustomerID(customerID);
				setName(rs.getString("NAME"));
				setAge(rs.getInt("AGE"));
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
				if (con != null) {
					con.close();
				}
			} catch (SQLException ex) {
				throw new EJBException(ex);
			}
		}
	}

	public void ejbStore() throws EJBException, RemoteException {
		CustomerID customerID = getCustomerID();
		Connection con = null;
		PreparedStatement ps = null;
		try {
			InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/Database");
			con = ds.getConnection();
			ps = con.prepareStatement("UPDATE CUSTOMERBEAN04 SET NAME = ? , AGE = ? WHERE CUSTOMERID = ?");
			ps.setString(1, name);
			ps.setInt(2, age);
			ps.setInt(3, customerID.getCustomerID());
			if (ps.executeUpdate() != 1) {
				throw new EJBException("False update Customer04 Bean...");
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
				if (con != null) {
					con.close();
				}
			} catch (SQLException ex) {
				throw new EJBException(ex);
			}
		}
	}

	public void ejbActivate() throws EJBException, RemoteException {
	}

	public void ejbPassivate() throws EJBException, RemoteException {
	}

	public void ejbRemove() throws RemoveException, EJBException, RemoteException {
		CustomerID customerID = getCustomerID();
		Connection con = null;
		PreparedStatement ps = null;
		try {
			InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/Database");
			con = ds.getConnection();
			ps = con.prepareStatement("DELETE FROM CUSTOMERBEAN04 WHERE CUSTOMERID = ?)");
			ps.setInt(1, customerID.getCustomerID());
			if (ps.executeUpdate() != 1) {
				throw new EJBException("False delete Customer04 Bean...");
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
				if (con != null) {
					con.close();
				}
			} catch (SQLException ex) {
				throw new EJBException(ex);
			}
		}
	}

}
